const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //设置验证码初始
    sendTime: '获取验证码',
    snsMsgWait: 60,
    tel: '',
    captch: '',
    loading: false,
    disabled: false,
    isOverShare: true
  },
  registershow() {
    this.setData({
      registerShow: true
    })
  },
  registerClose() {
    this.setData({
      registerShow: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
  },
  /**
   * @xk 获取注册协议
   */
  getInfo: function(){
    var that = this;
    https.post('getSystemInfo',{}).then(res=>{
      that.setData({
        info: res
      })
    })
  },
  /**
   * 手机号输入
   */
  phoneInput(e) {
    this.data.tel = e.detail.value
  },
  captchInput(e) {
    this.data.captch = e.detail.value
  },
  // 获取验证码
  sendCode: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id'),
      tel = this.data.tel;
    if (user_id <= 0) {
      wx.redirectTo({
        url: '/pages/grant/grant',
      })
    }
    if (!tel || tel == '') {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    https.post('sendCode', {
      user_id: user_id,
      tel: tel
    }, false, '验证码获取中').then(res => {
      if (res) {
        // 60秒后重新获取验证码
        var inter = setInterval(function () {
          this.setData({
            smsFlag: true,
            sendTime: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              sendTime: '获取验证码',
              snsMsgWait: 60,
              smsFlag: false
            });
          }
        }.bind(this), 1000);
      }
    })
  },
  /**
   * @xk 绑定手机号
   * @param {} obj
   */
  formSumit: function () {
    var that = this,
      user_id = wx.getStorageSync('user_id'),
      // captch = this.data.captch,
      tel = this.data.tel;
    this.setData({
      loading: true,
      disabled: true
    });
    https.post('bindTel', {
      user_id: user_id,
      tel: tel,
      // captch: captch,
      pid: wx.getStorageSync('l_id'),
      timestr: wx.getStorageSync('timestr'),
      act: 'register'
    }, false, '正在绑定').then(res => {
      if (res) {
        app.globalData.tel = tel;
        app.globalData.openid = res.openid;
        wx.setStorageSync('user_info');
        wx.setStorageSync('tel', tel);
        wx.setStorageSync('openid', res.openid);
        app.globalData.login_status = true;
        wx.setStorageSync('login_status', true);
        setTimeout(() => {
          console.log('login_status', wx.getStorageSync('login_status'));
          let page = getCurrentPages()
          console.log(page)
          let route = page[page.length - 2].route //上一页地址
          console.log(route)
          const options = page[page.length - 2].options
          if (route == 'pages/my/my' || route == 'pages/home/home' || route == 'pages/shopping_mall/shopping_mall') {
            wx.redirectTo({
              url: '/' + route,
            })
          } else {
            if (options) {
              route += '?';
              for (let key in options) {
                const value = options[key]
                route += `${key}=${value}&`
              }
            }
            console.log('进入了这里')
            wx.navigateBack()
          }
        }, 1500);

      } else {
        this.setData({
          loading: false,
          disabled: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // https.clearLoginState();
    console.log('signin onUnload')
    if(!app.globalData.tel) {
      let page = getCurrentPages()
      const backLength = page.length >=3 ? 3 : 2
      console.log('backLength', backLength)
      let route = page[page.length - backLength].route //上一页地址
      const options = page[page.length - backLength].options
      console.log(route)
      if (route == 'pages/my/my' || route == 'pages/home/home' || route == 'pages/shopping_mall/shopping_mall') {
        wx.reLaunch({
          url: '/' + route,
        })
      } else {
        if (options) {
          route += '?';
          for (let key in options) {
            const value = options[key]
            route += `${key}=${value}&`
          }
        }
        wx.reLaunch({
          url: '/' + route
        })
    }
    }
  },
})
